package com.credithc.signature.core.fadada;

import com.alibaba.fastjson.JSONObject;
import com.credithc.signature.core.service.signprovide.fadada.FadadaSdkClient;
import com.fadada.fosclient.util.random.UUIDGenerator;
import com.fadada.lightapi.FDDSDKClient;
import com.fadada.lightapi.beans.contract.ContractDownloadRequest;
import com.fadada.lightapi.beans.contract.ContractDownloadResponse;
import com.fadada.lightapi.beans.contract.UploadContractReq;
import com.fadada.lightapi.beans.contract.UploadContractRsp;
import com.fadada.lightapi.beans.contract.ViewContractReq;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2020年07⽉07⽇<br/>
 * @version：v1.0.0<br>
 */
@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class ContractDemo {

    @Autowired
    FadadaSdkClient fadadaSdkClient;

    @Test
    public void test() throws Exception {
        UploadContractReq req = new UploadContractReq();
        req.setContractId("123456");
        req.setDocTitle("测试sdk上传合同");
        req.setDocType(".pdf");
//        req.setDocUrl("http://static.fadada.com/fortest20170901/test_12.pdf");
        req.setFile(new File("D:/downloadFile.pdf"));
        FDDSDKClient client = FadadaSdkClient.initClient();
        UploadContractRsp rsp = client.uploadContract(req);
        //输出结果
        System.out.println(rsp.toString());
        //获取code、msg等输出参数(具体输出参数参考每个接口)
        String code = rsp.getCode();
        String msg = rsp.getMsg();
    }

    @Test
    public void test1() throws Exception {
        FDDSDKClient client = FadadaSdkClient.initClient();
        ViewContractReq req = new ViewContractReq();
        req.setContractId("123456");
        String viewContractUrl = client.getViewContractUrl(req);
        System.out.println(viewContractUrl);
    }

    @Test
    public void test2() throws Exception {
        FDDSDKClient client = FadadaSdkClient.initClient();
        UploadContractReq req7 = new UploadContractReq();
        req7.setContractId("12345678901");
        req7.setDocTitle("test");
        req7.setDocType(".pdf");
        req7.setFile(new File("D://test.pdf"));

        UploadContractRsp contractRsp = client.uploadContract(req7);
        log.info(contractRsp.toString());
    }

    @Test
    public void test3() throws Exception {
        FDDSDKClient client = FadadaSdkClient.initClient();
        ContractDownloadRequest d = new ContractDownloadRequest();
        d.setContractId("12345678901");
        ContractDownloadResponse res = client.contractDownload(d);
//        System.out.println(res.toString());
//        byte[] b = res.getContent();
//        log.info(new String(b));
    }

    @Test
    public void test4() throws Exception {
        UploadContractReq uploadContractReq = new UploadContractReq();
        String uuid = UUIDGenerator.getUUID();
        System.out.println(uuid);
        uploadContractReq.setContractId(uuid);
        uploadContractReq.setDocTitle("生成合同_8041adsfsd3");
        uploadContractReq.setDocType(".pdf");
        File file = new File("D://test.pdf");
        uploadContractReq.setFile(file);
//        StorageService service = new FadadaStorageService();
//        UploadContractRsp uploadContract = new FDDSDKClient(service).uploadContract(uploadContractReq);
        FDDSDKClient client = FadadaSdkClient.initClient();
        UploadContractRsp uploadContract = client.uploadContract(uploadContractReq);
        System.out.println("合同上传返回结果== " + JSONObject.toJSONString(uploadContract));
    }

    @Test
    public void test5() throws Exception {
        FDDSDKClient client = FadadaSdkClient.initClient();
        ContractDownloadRequest d = new ContractDownloadRequest();
        d.setContractId("c4c21944e42641dba61b084bfdd0d742");
        ContractDownloadResponse res = client.contractDownload(d);
        System.out.println(res.toString());
    }

}
